﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shake : MonoBehaviour
{
    private float edx_wobbleMin = 0.001f;
    private float edx_wobbleDamp = 0.9f;
    private bool isShaking = false;
    public float frequency = 1;

    void OnMouseDown()
    {
        StartCoroutine(Wobble(frequency));
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            if (isShaking) return;
            StartCoroutine(Wobble(frequency));
        }
    }
    private IEnumerator Wobble(float size)
    {
        Vector3 basePos = transform.localPosition;
        isShaking = true;
        while (size > edx_wobbleMin)
        {
            size *= edx_wobbleDamp;
            Vector3 newPos = basePos;
            newPos.x += Random.Range(-size, size);
            newPos.y += Random.Range(-size, size);
            newPos.z += Random.Range(-size, size);
            transform.localPosition = newPos;
            yield return null;
        }
        isShaking = false;
        transform.localPosition = basePos;
    }
}
